/*
============================================================================
 Name			: BlueToothServiceAdvertiser.h
 Author	  		: 
 Copyright   	: 
 Description 	: CBluetoothServiceAdvertiser has been implemented as a state machine that incapsulates
 				  the BT stack part responsible for advertising the BT service using the SDP server

				  The CBluetoothServiceAdvertiser's state machine includes the following operations:
				  1) Connecting to the SDP server and opening a SDP's RSdpDatabase subsession
				  2) Building the RPS's service record.
				  3) Notifying the observer when the service advertising is completed.

				  Note that if at any stage we receive an error then the state machine is reset to the EIdle
				  state and all resources previously allocated are closed.
============================================================================
*/

#ifndef __BLUETOOTHSERVICEADVERTISER_H__
#define __BLUETOOTHSERVICEADVERTISER_H__

// INCLUDES
#include <bt_sock.h>
#include <btmanclient.h>
#include <btsdp.h>
#include "bluetoothresponder.h"
#include "commoninterfaces.h"

// CLASS DECLARATION
/**
* CBluetoothSockConnector
* Advertise the BT service using the SDP server
*/
class CBluetoothServiceAdvertiser : public CActive
    {
public:
	/**
	* TAdvertiserState
	* State Machine
	*/
	enum TAdvertiserState
		{
	    EIdle,					//State machine entry point
	    EConnecting,			//Connecting to the SDP server
	    EBuildingServiceRecord,	//Creating an SDP record
	    };
public:
	/**
	* Constructor
	* @aParam aSerAdvObs Interface to notify the status of the service advertising
	*/
    CBluetoothServiceAdvertiser(MBluetoothServiceAdvertiserObserver& aSerAdvObs);
    
	/**
	* Destructor
	*/
    ~CBluetoothServiceAdvertiser();
    
	/**
	* Kicks off the state machine
	* @aparam aPort RFCOMM's port
	*/
	void StartL(const TInt aPort);
	
private:
	// from CSocketConnector
	/**
	* Handles CBluetoothServiceAdvertiser's state machine completion events
	*/
	virtual void RunL();
	
	/**
	* Not used
	*/
	virtual void DoCancel();
	
	/**
	* Handles a leave occurring in the request completion event handler RunL()
	* @param aError	 The leave code
	* @return TInt	 The default implementation returns aError.A derived class implementation should return KErrNone,
	*				 if it handles the leave; otherwise it should return any suitable value to cause the handling
	*				 of the error to be propagated back to the active scheduler.
	*/
	TInt RunError(TInt aError);
	
	/**
	* Call User::RequestComplete on this active object
	*/
	void SelfComplete();
	
	/**
	* Connects to the SDP server
	*/
	void ConnectToSdpL();
	
	/**
	* Creates an SDP record in the SDP's database
	*/
	void BuildServiceRecordL();
	
	/**
	* Deletes an SDP's record from the SDP's database
	*/
	void UnregisterService();

private:
	/**
	* Interfaces to notify the status of the service advertising
	*/
	MBluetoothServiceAdvertiserObserver& iSerAdvObs;
	
	/**
	* RFCOMM's port
	*/
	TInt iPort;
	
	/**
	* State Machine
	*/
	TAdvertiserState iState;
	
	/**
	* SDP session
	*/
	RSdp iSdpServ;
	
	/**
	* SDP database handle
	*/
	RSdpDatabase iSdpDatabase;
	
	/**
	* Subsession to the SDP through which service records and their attributes can be added, deleted, and updated
	*/
	TSdpServRecordHandle iRecordHandle;
    };

#endif //__BLUETOOTHSOCKCONNECTOR_H__
